package com.youyi.yychosesdk.Builder;

import android.app.Activity;
import android.net.Uri;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import com.youyi.yychosesdk.callback.SelectCallback;
import com.youyi.yychosesdk.constant.Type;
import com.youyi.yychosesdk.engine.ImageEngine;
import com.youyi.yychosesdk.models.album.entity.Photo;
import com.youyi.yychosesdk.result.Result;
import com.youyi.yychosesdk.setting.Setting;
import com.youyi.yychosesdk.ui.EasyPhotosActivity;
import com.youyi.yychosesdk.utils.result.EasyResult;
import com.youyi.yychosesdk.utils.uri.UriUtils;
import java.io.File;
import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;

public class AlbumBuilder {
    private static final String TAG = "com.youyi.yychosesdk";
    private static AlbumBuilder instance;
    private WeakReference<Activity> mActivity;
    private WeakReference<Fragment> mFragment;
    private WeakReference<Fragment> mFragmentV;
    private StartupType startupType;

    /* access modifiers changed from: private */
    public enum StartupType {
        CAMERA,
        ALBUM,
        ALBUM_CAMERA
    }

    private AlbumBuilder(Activity activity, StartupType startupType2) {
        this.mActivity = new WeakReference<>(activity);
        this.startupType = startupType2;
    }

    private AlbumBuilder(Fragment fragment, StartupType startupType2) {
        this.mFragment = new WeakReference<>(fragment);
        this.startupType = startupType2;
    }

    private AlbumBuilder(FragmentActivity fragmentActivity, StartupType startupType2) {
        this.mActivity = new WeakReference<>(fragmentActivity);
        this.startupType = startupType2;
    }

    private static AlbumBuilder with(Activity activity, StartupType startupType2) {
        clear();
        AlbumBuilder albumBuilder = new AlbumBuilder(activity, startupType2);
        instance = albumBuilder;
        return albumBuilder;
    }

    private static AlbumBuilder with(Fragment fragment, StartupType startupType2) {
        clear();
        AlbumBuilder albumBuilder = new AlbumBuilder(fragment, startupType2);
        instance = albumBuilder;
        return albumBuilder;
    }

    private static AlbumBuilder with(FragmentActivity fragmentActivity, StartupType startupType2) {
        clear();
        AlbumBuilder albumBuilder = new AlbumBuilder(fragmentActivity, startupType2);
        instance = albumBuilder;
        return albumBuilder;
    }

    public static AlbumBuilder createCamera(Activity activity) {
        return with(activity, StartupType.CAMERA);
    }

    public static AlbumBuilder createCamera(FragmentActivity fragmentActivity) {
        return with(fragmentActivity, StartupType.CAMERA);
    }

    public static AlbumBuilder createCamera(Fragment fragment) {
        return with(fragment, StartupType.CAMERA);
    }

    public static AlbumBuilder createAlbum(Activity activity, boolean z, ImageEngine imageEngine) {
        if (Setting.imageEngine != imageEngine) {
            Setting.imageEngine = imageEngine;
        }
        if (z) {
            return with(activity, StartupType.ALBUM_CAMERA);
        }
        return with(activity, StartupType.ALBUM);
    }

    public static AlbumBuilder createAlbum(FragmentActivity fragmentActivity, boolean z, ImageEngine imageEngine) {
        if (Setting.imageEngine != imageEngine) {
            Setting.imageEngine = imageEngine;
        }
        if (z) {
            return with(fragmentActivity, StartupType.ALBUM_CAMERA);
        }
        return with(fragmentActivity, StartupType.ALBUM);
    }

    public static AlbumBuilder createAlbum(Fragment fragment, boolean z, ImageEngine imageEngine) {
        if (Setting.imageEngine != imageEngine) {
            Setting.imageEngine = imageEngine;
        }
        if (z) {
            return with(fragment, StartupType.ALBUM_CAMERA);
        }
        return with(fragment, StartupType.ALBUM);
    }

    public AlbumBuilder setFileProviderAuthority(String str) {
        Setting.fileProviderAuthority = str;
        return this;
    }

    public AlbumBuilder setCount(int i) {
        if (Setting.complexSelector) {
            return this;
        }
        Setting.count = i;
        return this;
    }

    public AlbumBuilder setUseWidth(boolean z) {
        Setting.useWidth = z;
        return this;
    }

    public AlbumBuilder complexSelector(boolean z, int i, int i2) {
        Setting.complexSelector = true;
        Setting.complexSingleType = z;
        Setting.complexVideoCount = i;
        Setting.complexPictureCount = i2;
        Setting.count = i + i2;
        Setting.showVideo = true;
        return this;
    }

    public AlbumBuilder setCameraLocation(int i) {
        Setting.cameraLocation = i;
        return this;
    }

    public AlbumBuilder setMinFileSize(long j) {
        Setting.minSize = j;
        return this;
    }

    public AlbumBuilder setMinWidth(int i) {
        Setting.minWidth = i;
        return this;
    }

    public AlbumBuilder setMinHeight(int i) {
        Setting.minHeight = i;
        return this;
    }

    public AlbumBuilder setSelectedPhotos(ArrayList<Photo> arrayList) {
        Setting.selectedPhotos.clear();
        if (arrayList.isEmpty()) {
            return this;
        }
        Setting.selectedPhotos.addAll(arrayList);
        Setting.selectedOriginal = arrayList.get(0).selectedOriginal;
        return this;
    }

    public AlbumBuilder setSelectedPhotos(ArrayList<Photo> arrayList, boolean z) {
        Setting.selectedPhotos.clear();
        Setting.isSequentialSelectedPhotos = z;
        if (arrayList.isEmpty()) {
            return this;
        }
        Setting.selectedPhotos.addAll(arrayList);
        Setting.selectedOriginal = arrayList.get(0).selectedOriginal;
        return this;
    }

    @Deprecated
    public AlbumBuilder setSelectedPhotoPaths(ArrayList<String> arrayList) {
        Setting.selectedPhotos.clear();
        ArrayList arrayList2 = new ArrayList();
        Iterator<String> it = arrayList.iterator();
        while (it.hasNext()) {
            String next = it.next();
            File file = new File(next);
            Uri uri = null;
            WeakReference<Activity> weakReference = this.mActivity;
            if (!(weakReference == null || weakReference.get() == null)) {
                uri = UriUtils.getUri(this.mActivity.get(), file);
            }
            WeakReference<Fragment> weakReference2 = this.mFragment;
            if (!(weakReference2 == null || weakReference2.get() == null)) {
                uri = UriUtils.getUri(this.mFragment.get().getActivity(), file);
            }
            WeakReference<Fragment> weakReference3 = this.mFragmentV;
            if (!(weakReference3 == null || weakReference3.get() == null)) {
                uri = UriUtils.getUri(this.mFragmentV.get().getActivity(), file);
            }
            arrayList2.add(new Photo(null, uri == null ? Uri.fromFile(file) : uri, next, 0, 0, 0, 0, 0, 0, null));
        }
        Setting.selectedPhotos.addAll(arrayList2);
        return this;
    }

    @Deprecated
    public AlbumBuilder setSelectedPhotoPaths(ArrayList<String> arrayList, boolean z) {
        Setting.selectedPhotos.clear();
        Setting.isSequentialSelectedPhotos = z;
        ArrayList arrayList2 = new ArrayList();
        Iterator<String> it = arrayList.iterator();
        while (it.hasNext()) {
            String next = it.next();
            File file = new File(next);
            Uri uri = null;
            WeakReference<Activity> weakReference = this.mActivity;
            if (!(weakReference == null || weakReference.get() == null)) {
                uri = UriUtils.getUri(this.mActivity.get(), file);
            }
            WeakReference<Fragment> weakReference2 = this.mFragment;
            if (!(weakReference2 == null || weakReference2.get() == null)) {
                uri = UriUtils.getUri(this.mFragment.get().getActivity(), file);
            }
            WeakReference<Fragment> weakReference3 = this.mFragmentV;
            if (!(weakReference3 == null || weakReference3.get() == null)) {
                uri = UriUtils.getUri(this.mFragmentV.get().getActivity(), file);
            }
            arrayList2.add(new Photo(null, uri == null ? Uri.fromFile(file) : uri, next, 0, 0, 0, 0, 0, 0, null));
        }
        Setting.selectedPhotos.addAll(arrayList2);
        return this;
    }

    public AlbumBuilder setOriginalMenu(boolean z, boolean z2, String str) {
        Setting.showOriginalMenu = true;
        Setting.selectedOriginal = z;
        Setting.originalMenuUsable = z2;
        Setting.originalMenuUnusableHint = str;
        return this;
    }

    public AlbumBuilder onlyVideo() {
        return filter(Type.VIDEO);
    }

    public AlbumBuilder onlyGif() {
        return filter(Type.GIF);
    }

    public AlbumBuilder filter(String... strArr) {
        Setting.filterTypes = Arrays.asList(strArr);
        return this;
    }

    public AlbumBuilder setGif(boolean z) {
        Setting.showGif = z;
        return this;
    }

    public AlbumBuilder setVideo(boolean z) {
        Setting.showVideo = z;
        return this;
    }

    public AlbumBuilder setVideoMinSecond(int i) {
        Setting.videoMinSecond = (long) (i * 1000);
        return this;
    }

    public AlbumBuilder setVideoMaxSecond(int i) {
        Setting.videoMaxSecond = (long) (i * 1000);
        return this;
    }

    public AlbumBuilder setCleanMenu(boolean z) {
        Setting.showCleanMenu = z;
        return this;
    }


    private void setSettingParams() {
        int i = startupType.ordinal();
        if (i == 1) {
            Setting.onlyStartCamera = true;
            Setting.isShowCamera = true;
        } else if (i == 2) {
            Setting.isShowCamera = false;
        } else if (i == 3) {
            Setting.isShowCamera = true;
        }
        if (!Setting.filterTypes.isEmpty()) {
            if (Setting.isFilter(Type.GIF)) {
                Setting.showGif = true;
            }
            if (Setting.isFilter(Type.VIDEO)) {
                Setting.showVideo = true;
            }
        }
        if (Setting.isOnlyVideo()) {
            Setting.isShowCamera = false;
            Setting.showGif = false;
            Setting.showVideo = true;
        }
    }

    public void start(int i) {
        setSettingParams();
        launchEasyPhotosActivity(i);
    }

    public void start(SelectCallback selectCallback) {
        setSettingParams();
        WeakReference<Activity> weakReference = this.mActivity;
        if (weakReference == null || weakReference.get() == null || !(this.mActivity.get() instanceof FragmentActivity)) {
            WeakReference<Fragment> weakReference2 = this.mFragmentV;
            if (weakReference2 == null || weakReference2.get() == null) {
                throw new RuntimeException("mActivity or mFragmentV maybe null, you can not use this method... ");
            }
            EasyResult.get(this.mFragmentV.get()).startEasyPhoto(selectCallback);
            return;
        }
        EasyResult.get((FragmentActivity) this.mActivity.get()).startEasyPhoto(selectCallback);
    }

    private void launchEasyPhotosActivity(int i) {
        WeakReference<Activity> weakReference = this.mActivity;
        if (weakReference == null || weakReference.get() == null) {
            WeakReference<Fragment> weakReference2 = this.mFragment;
            if (weakReference2 == null || weakReference2.get() == null) {
                WeakReference<Fragment> weakReference3 = this.mFragmentV;
                if (weakReference3 != null && weakReference3.get() != null) {
                    EasyPhotosActivity.start(this.mFragmentV.get(), i);
                    return;
                }
                return;
            }
            EasyPhotosActivity.start(this.mFragment.get(), i);
            return;
        }
        EasyPhotosActivity.start(this.mActivity.get(), i);
    }

    private static void clear() {
        Result.clear();
        Setting.clear();
        instance = null;
    }
}
